package com.homework.HomeAcount.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.homework.HomeAcount.entity.Resource;
import com.homework.HomeAcount.entity.TreeNode;
import com.homework.HomeAcount.service.ResourceService;
/**
 * 资源操作相关Controller
 * Project Name:HomeAcount 
 * @author gk
 * TODO
 * Date:2015年12月23日上午10:56:45 
 * Copyright (c) 2015, gkaigk@126.com All Rights Reserved. 
 * @Version 1.0
 */
@Controller
@RequestMapping("/resource")
public class ResourceController {
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/loadResource")
	public String loadPageResource(Model model) {
		List<Resource> list = resourceService.getParentResources();
		model.addAttribute("pResource", list);
		return "/resource/resource";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/loadResourceTree")
	@ResponseBody
	public List<TreeNode> loadResourceTree() {
		List<TreeNode> list = resourceService.getResourceTreeNodes();
		return list;
	}
	
	@RequestMapping("/getResourceById")
	@ResponseBody
	public Resource getResourceById(String id) {
		return resourceService.getResourceById(id);
	}
	
	@RequestMapping("/resourceSave")
	@ResponseBody
	public Map<String, Object> resourceSave(Resource resource,String opFlag) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if("1".equals(opFlag)) {
				resourceService.insertResource(resource);
			}else {
				resourceService.updateResourceById(resource);
			}
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", false);
		}
		return map;
	}
}
